package com.borqsprocessmanager.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.borqsprocessmanager.App;

public class PowerProfileUtil {
    private final String TAG = "PowerProfileUtil";

    private final String CLASSNAME = "com.android.internal.os.PowerProfile";
    private final String GET_AVERAGE_POWER = "getAveragePower";

    private Class hideClass = null;
    private Object invokeOperation = null;

    public final String[] TAGS = { "none", "cpu.idle", "cpu.awake",
            "wifi.scan", "wifi.on", "wifi.active", "gps.on", "bluetooth.on",
            "bluetooth.active", "bluetooth.at", "screen.on", "radio.scanning",
            "radio.active", "screen.full", "dsp.audio", "dsp.video",
            "battery.capacity", "cpu.active", "radio.on", "cpu.speeds", };

    public PowerProfileUtil() {
        try {
            hideClass = Class.forName(CLASSNAME);
            Constructor[] cons = hideClass.getConstructors();
            invokeOperation = cons[0].newInstance(App.mContext);

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public float getBatteryCapacity() {
        String BatteryCapacity = getAveragePower(TAGS[16]);
        Log.v(TAG, "BatteryCapacity===" + BatteryCapacity);
        return Float.parseFloat(BatteryCapacity);
    }

    public String getAveragePower(String tag) {
        Method method;
        String sresult = "";
        try {
            method = hideClass.getMethod(GET_AVERAGE_POWER, String.class);
            Object result = method.invoke(invokeOperation, tag);
            sresult = result.toString();

        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sresult;
    }

    private String getAveragePower(String tag, int level) {
        Method method;
        String sresult = "";
        try {
            method = hideClass.getMethod(GET_AVERAGE_POWER, String.class,
                    int.class);
            Object result = method.invoke(invokeOperation, tag, level);
            sresult = tag + " = " + result.toString() + "\n";

        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sresult;
    }
}
